package com.wxxymaker.edu.service;

import com.blade.ioc.annotation.Bean;
import com.blade.kit.StringKit;
import com.wxxymaker.edu.model.entity.User;
import com.wxxymaker.edu.utils.TimeUtils;
import io.github.biezhi.anima.Anima;
import io.github.biezhi.anima.core.AnimaQuery;
import io.github.biezhi.anima.enums.OrderBy;
import io.github.biezhi.anima.page.Page;
import lombok.extern.slf4j.Slf4j;

/**
 * @AUTHOR soft
 * @DATE 2018/9/4 16:02
 * @DESCRIBE 用户业务层
 */
@Slf4j
@Bean
public class UserService implements Service<User> {

    /**
     * 用户注册
     * 需要填入注册时间
     * @param user 注册用户
     * @return 注册的用户对象包涵id
     */
    @Override
    public User add(User user) {
        user.setPassword(user.getMd5Pass());
        user.setRegTime(TimeUtils.getTime());
        boolean rollback = Anima.atomic(() -> {
            Integer pk = user.save().asInt();
            user.setId(pk);
        }).isRollback();
        return rollback ? null : user;
    }

    /**
     * 传入登录用户,返回登录验证结果,是否成功
     * 验证成功则记录下登录时间loginTime
     * @param user 用户信息,带用户名和密码  以及审核状态
     * @return 登录用户信息 否则null
     */
    public User loginCheck(User user) {
        AnimaQuery<User> where = Anima.select().from(User.class)
                .where(User::getPassword, user.getMd5Pass());

        User sqlUser = where.where(User::getUsername, user.getUsername()).one();
        if (sqlUser == null) {
            sqlUser = where.where(User::getEmail, user.getUsername()).one();
        }

        if (sqlUser != null) {
            int id = sqlUser.getId();
            sqlUser.setLoginTime(TimeUtils.getTime());
            sqlUser.update();
            sqlUser.setId(id);
        }

        return sqlUser;
    }


    /**
     *
     * @param email 根据email找回密码以及禁止邮箱重复
     * @return
     */
    public User findEmail(String email) {
        return Anima.select().from(User.class)
                .where("email",email).one();

    }

    /**
     *
     * @param username 根据username禁止账号重复
     * @return
     */
    public User findUser(String username) {
        return Anima.select().from(User.class)
                .where("username", username).one();
    }

    /**
     * 分页查询用户
     * @param page 页码
     * @param limit 尺寸
     * @param query 条件
     */
    public Page<User> page(int page, int limit, User query) {
        AnimaQuery<User> from = Anima.select().from(User.class).order(User::getId, OrderBy.DESC);
        if (query != null) {
            if (query.getRole() != null) {
                if (query.getRole() == 2) {
                    from.in(User::getRole,  2, 3);
                } else {
                    from.where(User::getRole, 1);
                }
            }
            if (query.getAudit() != null) {
                from.where(User::getAudit, query.getAudit());
            }
            if (query.getName() != null) {
                from.like(User::getName, query.getName());
            }
            if (query.getSex() != null) {
                from.where(User::getSex, query.getSex());
            }
        }
        return from.page(page, limit);
    }

    public boolean updateUser(User user) {
        AnimaQuery<User> animaQuery = Anima.update().from(User.class).where(User::getId, user.getId());
        if (user.getSex() != null) {
            animaQuery.set("sex", user.getSex());
        }
        if (user.getName() != null) {
            animaQuery.set("name", user.getName());
        }
        if (user.getTitle() != null) {
            animaQuery.set("title", user.getTitle());
        }
        if (user.getIntro() != null) {
            animaQuery.set("intro", user.getIntro());
        }
        if (user.getTitle() != null) {
            animaQuery.set("title", user.getTitle());
        }
        if (user.getSign() != null) {
            animaQuery.set("sign", user.getSign());
        }
        if (user.getTel() != null) {
            animaQuery.set("tel", user.getTel());
        }
        if (!StringKit.isEmpty(user.getIcon())) {
            animaQuery.set("icon", user.getIcon());
        }
        int i = animaQuery.execute();
        return i > 0;
    }


    @Override
    public User delete(Object pk) {
        return null;
    }

    @Override
    public boolean change(User user) {
        return user.update() > 0;
    }



    @Override
    public User byId(Object pk) {
        return Anima.select().from(User.class).where(User::getId, pk).one();
    }

    /**
     * 平台下用户统计
     * @param query 统计条件  role audit
     */
    public long count(User query) {
        AnimaQuery<User> from = Anima.select().from(User.class);
        if (query != null) {
            if (query.getRole() != null) {
                from.where(User::getRole, query.getRole());
            }
            if (query.getAudit() != null) {
                from.where(User::getAudit, query.getAudit());
            }
            if (query.getSex() != null) {
                from.where(User::getSex, query.getSex());
            }
        }
        return from.count();
    }
}
